﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SmApiLibrary.libs;
using CM = System.Configuration.ConfigurationManager;
using SmAp.Operator.OP.ABF;
using SmAp.Operator.Interface;
using SmApiLibrary.datatypes.Interface;
using SmApiLibrary.datatypes.AbstarctFactory;
using SmApiLibrary.helps;
using Teein.LiNing.BLL;
using SmApiLibrary.Err;
using System.Web.SessionState;

namespace Teein.H2O.Web
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class ScoreHandler : IHttpHandler, IRequiresSessionState
    {
        private int UserScore;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Session["oauth_token"] == null)
            {
                context.Response.Redirect("index.html");
            }
            //ScoreHandler.ashx?action=testing
            string action = context.Request.QueryString["action"];

            switch (action)
            {
                case "tested":
                    Tested(context);
                    break;
                case "getuserdisplay":
                    GetUserDisplay(context);
                    break;
            }
        }

        private void GetUserDisplay(HttpContext context)
        {
            context.Response.Clear();
            context.Response.Write(string.Format("{{'displayname':'{0}'}}", User.screen_name));
            context.Response.End();
        }

        protected IUser User
        {
            get
            {
                if (HttpContext.Current.Session["user"] != null)
                {
                    IUser user = HttpContext.Current.Session["user"] as IUser;
                    return user;
                }
                else
                {
                    HttpContext.Current.Response.Redirect("index.html");
                }
                return null;
            }
        }


        protected void Tested(HttpContext context)
        {

            string resultscoredetail = context.Request.QueryString["resultscoredetail"];
            string[] scores = null;
            if (!string.IsNullOrEmpty(resultscoredetail))
            {
                scores = resultscoredetail.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            }

            this.UserScore = (int)(scores.Length + 1)/2;

            AccountBLL aBll = new AccountBLL();
            int accountId = aBll.SelectAccountIdByUid(this.User.id);

            ScoreBLL sBll = new ScoreBLL();
            sBll.InsertScore(this.UserScore, accountId, context.Request.QueryString["resultscoredetail"]);

            context.Response.Clear();
            context.Response.Write(string.Format("{{'url':'{0}'}}","result.html?score=" + this.UserScore.ToString()));
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
